//
//  MainViewController.swift
//  SinaWeibo
//
//  Created by 郭平 on 15/9/30.
//  Copyright © 2015年 郭平. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let tab = MainTabBar()
        
            tab.plusButtonClickBlock = {
            printLog("中间按钮点击")
        }
        // 把tab 添加到tabBar里
        setValue(tab, forKey: "tabBar")
        
        addChildViewControllers()

    }
    
    func addChildViewControllers() {
        
        addchildViewController("Home", title: "首页", imageName: "tabbar_home", index: 0)
        addchildViewController("Message", title:"消息", imageName: "tabbar_message_center", index: 1)
        addchildViewController("Discover", title:"发现", imageName: "tabbar_discover", index: 2)
        addchildViewController("Profile", title:"我", imageName: "tabbar_profile", index: 3)
    }
    
    // 初始化单个导航控制器
    func addchildViewController(sbName: String, title: String, imageName: String, index: Int) {
        
        let item = GPSTabBarItem()
        item.index = index
        
        let sb = UIStoryboard(name: sbName, bundle: nil)
        let nav = sb.instantiateInitialViewController() as! UINavigationController
        nav.tabBarItem = item
        
        nav.topViewController?.title = title
        nav.tabBarItem.image = UIImage(named: imageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        nav.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        let titleColor = [NSForegroundColorAttributeName : UIColor.orangeColor()]
        
        nav.tabBarItem.setTitleTextAttributes(titleColor, forState:UIControlState.Selected)
        
        addChildViewController(nav)
    }
    
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        
        let iwItem = item as! GPSTabBarItem
        
        var index = 0
        
        for tabBarChild in tabBar.subviews {
        
            if tabBarChild.isKindOfClass(NSClassFromString("UITabBarButton")!){
                if index == iwItem.index {
                    for tabBarButtonChild in tabBarChild.subviews {
                        if tabBarButtonChild.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!) {
                            //拿到显示image的控件了
                            tabBarButtonChild.transform = CGAffineTransformMakeScale(0.8, 0.8)
                            
                            UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.1, initialSpringVelocity: 0, options: [], animations: { () -> Void in
                                tabBarButtonChild.transform = CGAffineTransformIdentity
                                
                                }, completion: { (finish) -> Void in
                                    
                            })
                        }
                    }
                }
                index++
            }
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
